package com.train.common.utils;

import com.train.common.exception.BusinessException;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Objects;

/**
 * 断言工具类
 * @author DaiGD
 */
public enum AssertUtils {
    ;
    /**
     * Assert a boolean expression, throwing a {@code BaseException}
     * if the expression evaluates to {@code false}.
     * @param expression a boolean expression
     * @param message the exception message to use if the assertion fails
     * @throws BusinessException if {@code expression} is {@code false}
     */
    public static void requireTrue(boolean expression, String message) {
        if (!expression) {
            throw new BusinessException(message);
        }
    }


    /**
     * Assert a boolean expression, throwing a {@code BaseException}
     * if the expression evaluates to {@code true}.
     * @param expression a boolean expression
     * @param message the exception message to use if the assertion fails
     * @throws BusinessException if {@code expression} is {@code true}
     */
    public static void requireFalse(boolean expression, String message) {
        if (expression) {
            throw new BusinessException(message);
        }
    }


    /**
     * 对象非空判断
     * @param obj 判断对象
     * @param message 异常信息
     */
    public static void requireNotNull(Object obj, String message) {
        if (Objects.isNull(obj)) {
            throw new BusinessException(message);
        }
    }

    /**
     * 字符串非空判断
     * @param str
     * @param message
     */
    public static void requireNotBlank(String str, String message) {
        if (StringUtils.isEmpty(str)) {
            throw new BusinessException(message);
        }
    }

    /**
     * 集合非空判断
     * @param collection
     * @param message
     */
    public static void requireNotEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(message);
        }
    }
}
